{
  "cells": [
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "# OpenAltimetry ICESat-2: Elevation Workflows\n\n\n### Overview\n<blockquote>This notebook pulls a subset of ATL06, ATL07, ATL08, ATL10, ATL12 or ATL13 elevation data from the ICESat 2 mission via the OpenAltimetry API. This API enables users to select a level 3a product, spatial region of interest and a track to get elevation data without the need to deal with large HDF files.</blockquote>\n\n\n\n### Instructions\n<blockquote>Get an API end point from any elevation plot in OpenAltimetry.org and paste it in the **OA_API_URL** below to start visualizing your data!\n\nLimitations - Date range is limited to 1 year. If no date range is specified, last 1 year's worth of data is returned. Spatial area is limited to 5x5 deg.</blockquote>\n\n\nNote that this notebook already has the Python libraries that you might need to do data analysis such as geopandas, plotly, fiona, shapely and more!"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "import pandas as pd\nimport json\nimport requests\nimport plotly.graph_objects as go\nfrom plotly.offline import iplot\n\n# We populate a list with the elevation data using the OpenAltimetry API\n\nprint('Requesting data from OA')\n\nOA_API_URL = 'https://openaltimetry.org/data/api/icesat2/level3a?product=atl08&startDate=2019-07-01&endDate=2019-12-30&minx=-102.27786&miny=35.49232&maxx=-101.91179&maxy=36.30839&trackId=752&client=jupyter&outputFormat=json'\n\ndataFrame = []\noa_plots = []\n\nr = requests.get(OA_API_URL)\nelevation_data = r.json()\n\nproduct = elevation_data[\"product\"]\n\ncolorChart = ['#66c2a5','#fc8d62','#8da0cb','#e78ac3','#a6d854','#ffd92f']\n\n#print (colorChart)\nprint('Product: ' + product)\nprint('TrackId: ' + elevation_data[\"trackId\"])\n\ncolorIndex = -1\n\nfor data_date in elevation_data['data']:\n    date = data_date[\"date\"]\n    scForward = data_date[\"scForward\"]\n    data_beams = data_date[\"beams\"]\n    \n    print('date: ' + date + '\\tscForward: ' + str(scForward) + '\\tTotal data beams: ' + str(len(data_beams)))\n\n    colorIndex += 1\n    colorIndex = colorIndex % 6\n    \n    for data_beam in data_beams:\n        beam = data_beam[\"beam\"]\n        isStrongBeam = data_beam[\"isStrongBeam\"]        \n        data_name = 'lat_lon_elev'\n        if product == 'ATL08':\n            data_name = 'lat_lon_elev_canopy'\n            \n        series = []            \n        for p in data_beam[data_name]:\n            series.append({\n                'lat': p[0],\n                'lon': p[1],\n                'h': p[2]\n            })\n\n        strong_beam = ''\n        if (isStrongBeam):\n            strong_beam = 'strong'\n        else:\n            strong_beam = 'weak'\n        \n        print ('\\tbeam: ' + beam + ',\\t' + strong_beam + ', \\tseries size: ' + str(len(series)))\n        \n        if (len(series) > 0):\n            df = pd.DataFrame.from_dict(series)\n            df.name = date + '_' + beam\n            df.color = colorChart[colorIndex]\n            \n            #print ('\\t\\tColor by date: ' + df.color)\n            dataFrame.append(df) \n    \nprint('dataFrame size: ' + str(len(dataFrame)))\n\nfor df in dataFrame:\n    oa_plots.append(go.Scatter3d(name=df.name, x=df['lat'], y=df['lon'], z=df['h'],\n                                    marker=dict(\n                                        size=1,\n                                        color=df.color\n                                    ),\n                                    line=dict(\n                                        color=df.color,\n                                        width=1\n                                    )\n                                )\n                   )\n\nlayout = go.Layout(\n    width=1200,\n    height=600,\n    scene = dict(aspectmode = 'manual', aspectratio =dict(x=1, y=1, z=0.5),\n                 xaxis=dict(title='Latitude'), yaxis=dict(title='Longitude'), zaxis=dict(title='Elevation (m)'))\n)\n\nprint('Plotting...')\n\nfig = go.Figure(data=oa_plots, layout=layout)\n\niplot(fig)\n    \n",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python36",
      "display_name": "Python 3.6",
      "language": "python"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "name": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.6",
      "file_extension": ".py",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
